package com.lm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.lm.model.InfoAccStateYes;
import com.lm.model.InfoBase;
import com.lm.model.InfoEconomy;
import com.lm.model.InfoFamily;
import com.lm.model.User;
import com.lm.service.ArmyinfoService;
import com.mysql.jdbc.StringUtils;

import freemarker.template.utility.StringUtil;

@Controller
@RequestMapping("/armyinfo")
public class ArmyinfoController {
	private static final Logger logger = LoggerFactory.getLogger(ArmyinfoController.class);
	@Autowired
	private ArmyinfoService armyinfoService;
	/**
	 * 跳转到新增军人信息页面
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/addInfo")
	public String addInfo(ModelMap modelMap) {
		return "/armyinfo/armyMessageForm";
	}
	/**
	 * 根据身份证id查询军人的信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="findById")
	@ResponseBody
	public Map<String, Object> findByIdcard(HttpServletRequest request) throws Exception{
		Map<String, Object> resultmap = new HashMap<String, Object>();
		String idcard = request.getParameter("idcard");
		InfoBase infoBase = armyinfoService.findInfoBaseByIdcard(idcard);
		System.out.println(idcard);
		return resultmap;
	}
	/**
	 * 查询所有的军人基本信息集合
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="findAll")
	@ResponseBody
	public Map<String, Object> findAll(HttpServletRequest request) throws Exception{
		Map<String, Object> resultmap = new HashMap<String, Object>();
		List<InfoBase> infolist  = new ArrayList<InfoBase>();
		infolist = armyinfoService.findAll();
		return resultmap;
	}
	/**
	 * 新增或修改基本信息表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="saveInfoBase")
	@ResponseBody
	public Map<String, Object> saveInfoBase(HttpServletRequest request) throws Exception{
		Map<String, Object> resultmap = new HashMap<String, Object>();
		InfoBase infoBase = new InfoBase();
			infoBase.setName(request.getParameter("name"));	
			infoBase.setIdcard(request.getParameter("idcard"));
			infoBase.setBirthday(request.getParameter("birthday"));
			infoBase.setSex(request.getParameter("sex"));
			infoBase.setTelphone(request.getParameter("telphone"));
			infoBase.setArmy_id(request.getParameter("army_id"));
			infoBase.setArmy_begin(request.getParameter("army_begin"));
			infoBase.setArmy_end(request.getParameter("army_end"));
			infoBase.setArmy_year(request.getParameter("army_year"));
			infoBase.setAddress(request.getParameter("address"));
			infoBase.setArmy_no(request.getParameter("army_no"));
			Integer result = armyinfoService.saveInfoBase(request,infoBase);
		if (result>0) {
			resultmap.put("code", "1");
			resultmap.put("msg", "success");
		}else {
			resultmap.put("code", "0");
			resultmap.put("msg", "error");
		}
		return resultmap;
	}
	/**
	 * 修改或新增家庭信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="saveInfoFamily")
	@ResponseBody
	public Map<String, Object> saveInfoFamily(HttpServletRequest request) throws Exception{
		Map<String, Object> resultmap = new HashMap<String, Object>();
		InfoFamily infoFamily = new InfoFamily();
		infoFamily.setIdcard(request.getParameter("idcard"));
		infoFamily.setType(request.getParameter("type"));
		infoFamily.setName(request.getParameter("familyname"));
		infoFamily.setWorkunit(request.getParameter("workunit"));
		Integer result = armyinfoService.saveInfoFamily(request,infoFamily);
		if (result>0) {
			resultmap.put("code", "1");
			resultmap.put("msg", "success");
		}else {
			resultmap.put("code", "0");
			resultmap.put("msg", "error");
		}
		return resultmap;
	}
	/**
	 * 修改或更新家庭经济状况
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="saveInfoEconomy")
	@ResponseBody
	public Map<String, Object> saveInfoEconomy(HttpServletRequest request) throws Exception{
		Map<String, Object> resultmap = new HashMap<String, Object>();
		InfoEconomy infoEconomy = new InfoEconomy();
		infoEconomy.setIdcard(request.getParameter("idcard"));
		infoEconomy.setEconomytype(request.getParameter("economytype"));
		infoEconomy.setDifficult_reasons(request.getParameter("difficult_reasons"));
		if (request.getParameter("idcard")==null || request.getParameter("idcard")=="") {
			resultmap.put("code", "0");
			resultmap.put("msg", "error");
			return resultmap;
		}
		Integer result = armyinfoService.saveInfoEconomy(request,infoEconomy);
		if (result>0) {
			resultmap.put("code", "1");
			resultmap.put("msg", "success");
		}else {
			resultmap.put("code", "0");
			resultmap.put("msg", "error");
		}
		return resultmap;
	}
	/**
	 * 保存符合政府安排工作条件的职业信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="saveInfoWorkeraccstateyes")
	@ResponseBody
	public Map<String, Object> saveInfoWorkeraccstateyes(HttpServletRequest request) throws Exception{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		InfoAccStateYes infoYes = new InfoAccStateYes();
		infoYes.setIdcard(request.getParameter("idcard"));
		infoYes.setArmy_idcard(request.getParameter("army_idcard"));
		infoYes.setDealwith_date(request.getParameter("dealwith_date"));
		infoYes.setDealwith_unit(request.getParameter("dealwith_date"));
		infoYes.setIswork_yesno(request.getParameter("iswork_yesno"));
		infoYes.setNowork_reason(request.getParameter("iswork_yesno"));
		infoYes.setDealwith_choicehelp(request.getParameter("dealwith_choicehelp"));
		infoYes.setEndowment_insurance(request.getParameter("endowment_insurance"));
		infoYes.setEpay_yesno(request.getParameter("epay_yesno"));
		infoYes.setEpay_state(request.getParameter("epay_state"));
		infoYes.setEpay_starttime(request.getParameter("epay_starttime"));
		infoYes.setEpay_endtime(request.getParameter("epay_endtime"));
		infoYes.setMedical_insurance(request.getParameter("medical_insurance"));
		infoYes.setMpay_yesno(request.getParameter("mpay_yesno"));
		infoYes.setMpay_state(request.getParameter("mpay_state"));
		infoYes.setMpay_starttime(request.getParameter("mpay_starttime"));
		infoYes.setMpay_endtime(request.getParameter("mpay_endtime"));
		infoYes.setWork_state(request.getParameter("work_state"));
		infoYes.setWork_leave(request.getParameter("work_leave"));
		infoYes.setWork_lreason(request.getParameter("work_lreason"));
		Integer result = armyinfoService.saveInfoAccStateYes(request,infoYes);
		return resultMap;
	}
}
